package com.anysoft.utils;

import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.text.TextUtils;
import android.widget.Toast;

import com.anysoft.MentorApplication;
import com.anysoft.MentorConst;
import com.anysoft.StaticField;
import com.anysoft.base.BaseActivity;
import com.anysoft.bean.BaseBean;
import com.anysoft.bean.ResultBean;
import com.anysoft.view.dialog.IcbcDialog;

import static com.anysoft.MentorConst.TIMEOUT_CODE;
import static com.anysoft.MentorConst.TIMEOUT_CODE_DS;

/**
 * Created by wwq on 2017/2/9.
 */

public class CommonUtil {

    private static Toast mToast = null;
    private static IcbcDialog dialog;

    /**
     * 检查状态JavaBean，主要检查是否超时，超时了内部会做相应处理，即跳转到登录
     *
     * @param activity
     * @param baseBean
     * @param result
     * @return 如果检查OK则返回true
     */
    public static boolean checkState(final BaseActivity activity, BaseBean baseBean, String result) {

        if (TextUtils.isEmpty(result) && !isNetWorkConnected(activity)) {
            showToast(activity, MentorConst.NO_NETWORK_TIP);
            return false;
        }

        if (baseBean == null
                || TextUtils.isEmpty(baseBean.getRespCode())) {
            showToast(activity, MentorConst.SERVER_BUSY_TIP);
            return false;
        }

        if (TIMEOUT_CODE.equals(baseBean.getRespCode()) || TIMEOUT_CODE_DS.equals(baseBean.getRespCode())) {
            // 如果超时
            MentorApplication application = (MentorApplication) activity.getApplication();
            clearLoginData(application);
            if (dialog == null) {
                dialog = new IcbcDialog(activity);
                dialog.setAnimationEnable(true);
                dialog.setCancelable(false);
                dialog.setTitle("提示");
                dialog.setContentText(baseBean.getRespMsg());
                dialog.setEditTextType(false);
                dialog.setPositiveListener("确定", new IcbcDialog.OnPositiveListener() {
                    @Override
                    public void onClick(IcbcDialog icbcDialog) {
                        icbcDialog.dismiss();
                        dialog = null;
                        ActivityUtils.gotoLogin(activity, activity.hasUser(), false, activity.findUserId(),true);
                    }
                }).show();
            }

            return false;
        }

        //风险评测过期

        if (!"0000".equals(baseBean.getRespCode())) {
            // 如果结果不OK
            activity.showConfirmAlert(activity, "提示", baseBean.getRespMsg());
            return false;
        }

        return true;
    }

    /**
     * 检查状态JavaBean，主要检查是否超时，超时了内部会做相应处理，即跳转到登录
     *
     * @param activity
     * @param baseBean
     * @param result
     * @return 返回结果对象
     */
    public static ResultBean checkStateBean(Activity activity, BaseBean baseBean, String result) {

        ResultBean resultBean = new ResultBean();
        resultBean.setResult(result);

        if (TextUtils.isEmpty(result) && !isNetWorkConnected(activity)) {
//            showToast(activity, MentorConst.NO_NETWORK_TIP);
            resultBean.setSuccess(false);
            resultBean.setTip(MentorConst.NO_NETWORK_TIP);
            return resultBean;
        }

        if (baseBean == null
                || TextUtils.isEmpty(baseBean.getRespCode())) {
            showToast(activity, MentorConst.SERVER_BUSY_TIP);

            resultBean.setSuccess(false);
            resultBean.setTip(MentorConst.NO_NETWORK_TIP);
            return resultBean;
        }

        if (TIMEOUT_CODE.equals(baseBean.getRespCode()) || TIMEOUT_CODE_DS.equals(baseBean.getRespCode())) {
            // 如果超时
            showToast(activity, MentorConst.TIMEOUT_TIP);

            resultBean.setSuccess(false);
            resultBean.setCode(baseBean.getRespCode());
            resultBean.setTip(MentorConst.TIMEOUT_TIP);

            MentorApplication application = (MentorApplication) activity.getApplication();
            clearLoginData(application);
            return resultBean;
        }

        if ("-4032".equals(baseBean.getRespCode()) || "-4058".equals(baseBean.getRespCode())) {
            resultBean.setSuccess(false);
            resultBean.setCode(baseBean.getRespCode());
            resultBean.setTip(baseBean.getRespMsg());

            return resultBean;
        }

        if (!"0000".equals(baseBean.getRespCode())) {
            // 如果结果不OK
            resultBean.setSuccess(false);
            resultBean.setCode(baseBean.getRespCode());
            resultBean.setTip(baseBean.getRespMsg());

            return resultBean;
        }

        resultBean.setSuccess(true);

        return resultBean;
    }

    /**
     * 根据是服务器忙还是没有可用网络来显示对应的错误信息
     *
     * @param context
     */
    public static void showErrorToast(Context context) {
        if (isNetWorkConnected(context)) {
            showToast(context, "当前不能连接到服务器，请稍后再试!");
        } else {
            showToast(context, "网络不可用，请检查后登录！");
        }
    }

    /**
     * 检查是否有能上网的网络连接存在
     *
     * @param mContext 当前Activity
     * @return true 表示有可用连接
     */
    public static boolean isNetWorkConnected(Context mContext) {
        ConnectivityManager manager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo info = manager.getActiveNetworkInfo();
        if (info != null && info.isConnected()) {
            return true;
        } else {
            ((BaseActivity) mContext).showConfirmAlert(mContext, "提示", "网络连接失败，请检查您的网络");
            ((BaseActivity) mContext).finish();
            return false;
        }
    }

    public static void showToast(Context contenxt, String msg) {
        if (mToast == null) {
            mToast = Toast.makeText(contenxt, msg, Toast.LENGTH_SHORT);
        } else {
            mToast.setText(msg);
        }
        mToast.show();
    }

    /**
     * 清除登录数据
     *
     * @param application
     */
    public static void clearLoginData(MentorApplication application) {
        application.userBean = null;
        application.accountLoginBean = null;
        StaticField.token = null;
    }
}
